import axios from 'axios';

export default {
    install: function(Vue, options) {

        let service = {            

            // 查询符合条件的比赛场次数量
            QuerySessionsCount: (condition) => {
                return axios.post('/admin/session/query/count', condition);
            },

            // 查询符合条件的比赛场次
            QuerySessions: (condition, pageIndex, pageSize) => {
                return axios.post(`/admin/session/query?pageIndex=${pageIndex}&pageSize=${pageSize}`, condition);
            },

            // 请求指定的比赛场次
            GetSessionById: (sessionId) => {
                return axios.get(`/admin/session/${sessionId}`);
            },

            // 创建比赛场次
            CreateSession: (session) => {
                return axios.post('/admin/session/create', session);
            },

            // 更新比赛场次信息
            ModifySession: (session) => {
                return axios.put('/admin/session', session);
            },

            // 保存比赛成绩审核人
            ModifyReviewer: (session) => {
                return axios.put(`/admin/session/${session.SessionId}/reviewer`);   
            },

            // 导出比赛成绩和名次
            ExportScore: (session) => {
                return axios.get(`/admin/session/${session.SessionId}/export/score`)
                    .then(ret => {
                        var link = aphf.file.ADDR + '/' + ret;
                        var a = document.createElement("a");
                        a.href = link;
                        a.click();
                    });
            },

            // 导出秩序册
            ExportOrderBook: (session) => {
                return axios.get(`/admin/session/${session.SessionId}/export/orderbook`)
                    .then(ret => {
                        var link = aphf.file.ADDR + '/' + ret;
                        var a = document.createElement("a");
                        a.href = link;
                        a.click();
                    }); 
            },

            // 导出秩序册
            ExportScoreSheet: (session) => {
                return axios.get(`/admin/session/${session.SessionId}/export/scoresheet`)
                    .then(ret => {
                        var link = aphf.file.ADDR + '/' + ret;
                        var a = document.createElement("a");
                        a.href = link;
                        a.click();
                    }); 
            },

            // 导出评委工作安排
            ExportJuries: () => {
                return axios.get(`/admin/session/export/juries`)
                    .then(ret => {
                        var link = aphf.file.ADDR + '/' + ret;
                        var a = document.createElement("a");
                        a.href = link;
                        a.click();
                    }); 
            },

            // 删除比赛场次
            RemoveSession: (session) => {
                return axios.delete(`/admin/session/${session.SessionId}`);
            },

            // 保存比赛成绩
            SaveScore: (competes) => {
                return axios.post('/admin/session_compete/modify/score', competes);
            },
            
            // 导出奖状文件
            ExportCertificate: (sessionCode, competes, template) => {
                return axios.post(`/admin/session_compete/export/${sessionCode}/certificate/${template}`, competes)
                    .then(ret => {
                        var link = aphf.file.ADDR + '/' + ret;

                        var a = document.createElement("a");
                        a.download = 'Certificate.doc';
                        a.href = link;
                        a.click();                        
                    })
            }
        }

        Vue.prototype.$SessionService = service;
    }
}